package com.hopu.v1.service.admin.classes.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopu.v1.exception.BusinessException;
import com.hopu.v1.mapper.admin.classes.EmploymentInfoMapper;
import com.hopu.v1.pojo.classes.EmploymentInfo;
import com.hopu.v1.pojo.classes.Student;
import com.hopu.v1.pojo.common.R;
import com.hopu.v1.service.admin.classes.EmploymentInfoService;
import com.hopu.v1.service.admin.classes.StudentService;
import com.hopu.v1.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
* @author admin
* @description 针对表【t_employment_info(就业信息表)】的数据库操作Service实现
* @createDate 2024-09-27 14:23:04
*/
@Service
public class EmploymentInfoServiceImpl extends ServiceImpl<EmploymentInfoMapper, EmploymentInfo>
    implements EmploymentInfoService {

    @Autowired
    private EmploymentInfoMapper employmentInfoMapper;

    @Autowired
    private StudentService studentService;

    /**
     * 分页条件查询就业信息
     * @param page
     * @param StudentVo
     * @return
     */
    @Override
    public Page<StudentVo> selectAll(Page<EmploymentInfo> page, StudentVo StudentVo) {
        return employmentInfoMapper.selectEmploymentInfoPage(page.setCurrent(page.getCurrent()),StudentVo);
    }

    /**
     * 添加就业信息
     * @param employmentInfo
     * @return
     */
    @Override
    public R addEmploymentInfoById(EmploymentInfo employmentInfo) {
        if (studentIdCheck(String.valueOf(employmentInfo.getStudentId()))){
            throw new BusinessException("该学号学生不存在！");
        }
        if (!timeCheck(employmentInfo)){
            throw new BusinessException("离职时间不能早于入职时间！");
        }
        Student student = new Student();
        student.setId(employmentInfo.getId());
        student.setStatusGraduation(0);
        student.setStatusGraduationTime(LocalDate.now());
        studentService.updateStudent(student);
        employmentInfo.setId(null);
        return R.ok("添加成功！",save(employmentInfo));
    }

    /**
     * 通过Id修改就业信息
     * @param employmentInfo
     * @return
     */
    @Override
    public R updateEmploymentInfoById(EmploymentInfo employmentInfo) {
        if (studentIdCheck(String.valueOf(employmentInfo.getStudentId()))){
            throw new BusinessException("该学号学生不存在！");
        }
        if (!timeCheck(employmentInfo)){
            throw new BusinessException("离职时间不能早于入职时间！");
        }
        return R.ok("修改成功！",employmentInfoMapper.updateById(employmentInfo));
    }

    /**
     * 学号校验
     * @param studentId
     * @return
     */
    @Override
    public boolean studentIdCheck(String studentId) {
        Student one = studentService.getOne(new LambdaQueryWrapper<Student>().eq(Student::getStudentId, studentId));
        return one == null;
    }


    /**
     * 入职离职时间校验
     * @param employmentInfo 包含入职和离职日期的信息
     * @return 如果入职日期在离职日期之前或与之相同，则返回true；否则返回false
     */
    @Override
    public boolean timeCheck(EmploymentInfo employmentInfo) {
        if (employmentInfo.getStartDate() != null && employmentInfo.getEndDate() != null) {
            // 入职日期不应该在离职日期之后
            System.out.println(employmentInfo.getStartDate().isBefore(employmentInfo.getEndDate()));
            return employmentInfo.getStartDate().isBefore(employmentInfo.getEndDate());
        }
        // 如果startDate或endDate为null，则认为是无效的日期信息，返回false
        return true;
    }
}




